package cn.semcorp.zhuhaizj.quartzJob;

import cn.semcorp.zhuhaizj.exception.ServiceException;
import cn.semcorp.zhuhaizj.mapper.UserMapper;
import cn.semcorp.zhuhaizj.pojo.vo.LoginUserListVO;
import cn.semcorp.zhuhaizj.pojo.vo.LoginUserVO;
import cn.semcorp.zhuhaizj.restful.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;


@Slf4j
public class TobeLoginJob implements Job {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        List<LoginUserListVO> list = userMapper.selectUserList();
        if (list.size() == 0){
            return;
        }
        // 定时任务待写
        log.info("定时任务:登录认证是否过期-开始执行");
        tobeLogin(list);
        log.info("定时任务:登录认证是否过期-执行完成!");
    }


    /**
     * 监控
     */
    public void tobeLogin(List<LoginUserListVO> list){
        // 查询所有已登录的账号
        for (LoginUserListVO user : list) {
            // 指定时间   取出时间存在小数精度
            LocalDateTime specifiedTime = LocalDateTime.parse(user.getGmtModified().substring(0,user.getGmtModified().length()-2), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

            // 当前时间
            LocalDateTime currentTime = LocalDateTime.now();

            // 计算两个时间之间的时间差
            Duration duration = Duration.between(specifiedTime, currentTime);

            // 获取时间差的秒数
            long diffInSeconds = duration.getSeconds();
            if (diffInSeconds >= 12 * 60 * 60){
                userMapper.updateLoginState(user.getId());
            }
        }
    }
}
